<?php


namespace app\siteadmin\model;


use think\facade\Db;
use think\Model;

class RoleUser extends Model
{
    //获取后台某角色列表
    public static function roleUserList($where,$pageIndex,$pageSize){
        return Db::name('role_user')
            ->alias('ru')
            ->field('u.uid,u.username')
            ->join('user u','u.uid = ru.uid')
            ->where($where)
            ->order('ru.id desc')
            ->page($pageIndex,$pageSize)
            ->select();
    }

    //获取后台某角色的总条数
    public static function roleUserCount($where){
        return Db::name('role_user')
            ->alias('ru')
            ->join('user u','u.uid = ru.uid')
            ->where($where)
            ->count();
    }

    //获取某账号的身份
    public static function roleUser($where){
        return Db::name('role_user')->where($where)->find();
    }

    //角色权限
    public function jurisdiction($where){
        $list = self::alias('r')
            ->field('r.id,r.rid,r.uid,m.id as m_id,m.rid as m_rid,m.mid as m_mid,me.id as meid ,me.superior_id as superior_id ,me.menu_name as menu_name,me.link as link,me.Icon as Icon,me.exhibition as exhibition')
            ->leftJoin('role_menu m','m.rid = r.rid')
            ->leftjoin('menu me','me.id = m.mid')
            ->where($where)
            ->order('m.id', 'desc')
            ->select();
        return $list;
    }


    //admin权限
    public function adminJurisdiction($where){
        $list = $this::name('menu')
            ->field('me.id as id,me.superior_id as superior_id ,me.menu_name as menu_name,me.link as link,me.Icon as Icon,me.exhibition as exhibition,me.component as component,me.sort as sort,b.b_name as b_name,b.b_iden as b_iden,b.mid')
            ->alias('me')
            ->leftjoin('button b','b.mid = me.id')
            ->where($where)
            ->order('me.sort', 'asc')
            ->select();
        return $list;
    }




    //角色用户查询
    public function ruList($where){
        $list = $this::name('role_user')
        ->field('ru.id,ru.uid,ru.rid,r.name')
        ->alias('ru')
        ->leftJoin('role r','r.id = ru.rid')
        ->where($where)
        ->order('ru.id', 'desc')
        ->select();
        return $list;
    }

    //角色用户修改
    public function ruEdit($id,$update){

        $in = $this::update($update, ['id' => $id]);
        return $in;
    }

    //角色用户添加
    public  function ruAdd(array $data){
        return self::create($data);
    }

    public static function salesmanList_old($where,$pageIndex,$pageSize){
        return Db::name('user')
            ->alias('u')
            ->field('u.uid,u.username,count(u2.uid) as `all`,any_value(admin_salesman_rule.contact) as rule_contact,any_value(admin_salesman_rule.link_up) as rule_link_up,any_value(admin_salesman_rule.potential) as rule_potential ,any_value(admin_salesman_rule.lose_contact) as rule_lose_contact,any_value(admin_salesman_rule.with_no_need_for) as rule_with_no_need_for,any_value(admin_salesman_rule.follow_up) as rule_follow_up,any_value(admin_salesman_rule.pay_merchant_gold) as rule_pay_merchant_gold,
count(if(u2.member_pay_status=1,u2.uid,null)) as member_pay_count,count(if(u2.admin_salesman_link_up_status=0 and u2.member_pay_status=0,u2.uid,null)) as contact,
count(if(u2.admin_salesman_link_up_status=1 and u2.member_pay_status=0,u2.uid,null)) as link_up,count(if(u2.admin_salesman_link_up_status=2 and u2.member_pay_status=0,u2.uid,null)) as potential ,
count(if(u2.admin_salesman_link_up_status=3 and u2.member_pay_status=0,u2.uid,null)) as lose_contact,count(if(u2.admin_salesman_link_up_status=4 and u2.member_pay_status=0,u2.uid,null)) as with_no_need_for,
count(if(u2.admin_salesman_link_up_status=5 and u2.member_pay_status=0,u2.uid,null)) as follow_up,count(if(u2.admin_salesman_link_up_status=6 and u2.member_pay_status=0,u2.uid,null)) as pay_merchant_gold'
            )
            ->leftjoin('role_user ru','u.uid = ru.uid')
            ->leftjoin('admin_salesman_rule ','admin_salesman_rule.admin_id = ru.uid')
            ->leftjoin('user u2','u.uid=u2.admin_salesman_id')
            ->where($where)
            ->group('u.uid')
            ->order('u.uid desc')
            ->page($pageIndex,$pageSize)
            ->select();
    }
        
    public static function salesmanList($sql){
        return Db::query($sql);
    }
}
